package com.fairytask.ui.item
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	
	import flash.display.Sprite;
	
	
	/**
	 * 神兵九宫格 
	 * @author xujh
	 * 
	 */
	public class NineEquipItem extends BaseItem
	{
		private var _equipMc	:Clip;	//武器背景图
		private var _nineArr	:Array	=	[];
		
		//=======播放动画用=======
		//高亮
		private var _highLight	:Sprite;
		//动画轴
		private var runLine		:TimelineLite;
		//动画缓存
		private var _playCache	:Array	=	[];
		//在播放动画
		private var _isPlay		:Boolean	=	false;
		
		//播放速度
		private const PLAY_SPEED	:Number	=	0.1;
		//消失速度
		private const HIDE_SPEED	:Number	=	0.1;
		//=======================
		
		//九格每格大小
		private var _nineSize	:int; 
		//格间隔
		private var _nineBlank	:int	=	5;
		//填充颜色
		private var _nineColor	:uint;
		//填充颜色透明度
		private var _nineAlpha	:Number	=	0.6;
		//装备id
		private var _equipStr	:String;
		
		//可以领取
		private var _canGet		:Boolean	=	false;
		//9格计数(初始化)
		private var _getCount	:int	=	0;
		private var _getArr		:Array	=	[];
		
		public function NineEquipItem(size:int=70,fillColor:uint=0xCCCCCC)
		{
			//数据
			_nineSize	=	size;
			_nineColor	=	fillColor;
			
			//初始化
			_equipMc	=	new Clip();
			addChild(_equipMc);
			initNine();
			
			//初始化高亮特效
			_highLight	=	new Sprite();
			_highLight.graphics.beginFill(0xFFFF00,0.8);
			_highLight.graphics.drawRect(0,0,_nineSize,_nineSize);
			_highLight.graphics.endFill();
			addChild(_highLight);
			_highLight.alpha	=	0;
		}
		
		
		/**
		 * 初始化九格 
		 * 
		 */
		private function initNine():void
		{
			for(var i:int=0;i<9;i++)
			{
				var mask	:Sprite	=	new Sprite();
				drawBG(mask);
				mask.x = (_nineSize+_nineBlank)*(i%3);
				mask.y = (_nineSize+_nineBlank)*Math.floor(i/3);
				addChild(mask);
				_nineArr.push(mask);
			}
		}
		
		/**
		 * 绘制格子蒙版 
		 * @param sp
		 * 
		 */
		private function drawBG(sp:Sprite):void
		{
			sp.graphics.clear();
			sp.graphics.beginFill(_nineColor,_nineAlpha);
			sp.graphics.drawRect(0,0,_nineSize,_nineSize);
			sp.graphics.endFill();
		}
		
		/**
		 * 获得，取消蒙版 
		 * @param sp
		 * 
		 */
		private function clearDraw(sp:Sprite):void
		{
			sp.graphics.clear();
		}
		
		/**
		 * 获取该格 
		 * @param id
		 * @param status 状态，0未获取，1已获取
		 * 
		 */
		public function getPiece(id:int,status:int):void
		{
			//在初始化状况下，直接点亮
			if(_getCount < 9)
			{
				if(status == 0)
				{
					drawBG(_nineArr[id] as Sprite);
				}
				else
				{
					_getArr.push(id);
					clearDraw(_nineArr[id] as Sprite);
				}
				_getCount++;
			}
			else if(status == 1)
			{
				//更新表格重复点亮数据
				if(_getArr.indexOf(id)!=-1)
				{
					return;
				}
				//播放点亮动画
				_getArr.push(id);
				playToPos(id);
			}
		}
		
		public function playToPos(pos:int):void
		{
			//已经在播放动画
			if(_isPlay)
			{
				_playCache.push(pos);
				return;
			}
			//播放点亮动画
			playHighLight(pos);
		}
		
		/**
		 * 播放高亮
		 * 
		 */
		private function playHighLight(pos:int):void
		{
			_isPlay	=	true;
			runLine	=	new TimelineLite({onComplete:playComplete,onCompleteParams:[pos]});
			//初始化位置
			_highLight.x	=	_nineArr[0].x;
			_highLight.y	=	_nineArr[0].y;
			//定义动画
			for(var i:int=0;i<pos+1;i++)
			{
//				runLine.append(new TweenLite(_highLight, HIDE_SPEED, {alpha:0, x:_nineArr[i].x, y:_nineArr[i].y}));
				runLine.append(new TweenLite(_highLight, 0, {alpha:1, x:_nineArr[i].x, y:_nineArr[i].y, delay:PLAY_SPEED}));
			}
			//最后位置闪烁2次
			for(i=0;i<3;i++)
			{
				runLine.append(new TweenLite(_highLight, 0, {alpha:1, x:_nineArr[pos].x, y:_nineArr[pos].y, delay:PLAY_SPEED*0.1}));
				runLine.append(new TweenLite(_highLight, HIDE_SPEED, {alpha:0, x:_nineArr[pos].x, y:_nineArr[pos].y, delay:PLAY_SPEED}));
			}
		}
		
		/**
		 * 播放完成 
		 * 
		 */
		private function playComplete(pos:int):void
		{
			runLine.clear();
			_highLight.alpha	=	0;
			clearDraw(_nineArr[pos] as Sprite);
			//检查播放列表是否都播放完毕
			if(_playCache.length > 0)
			{
				playHighLight(_playCache.shift());
			}
			else
			{
				_isPlay	=	false;
				//最后确认是否可以领取
				if(_canGet)
				{
					_equipMc.filters	=	[Filter.YELLOW_INNER];
				}
			}
		}
		
		/**
		 * 设置装备（底图） 
		 * @param val
		 * 
		 */
		public function set equipId(val:String):void
		{
			_equipStr		=	val;
//			_equipMc.source	=	ResConfig.iconPath + "40010300.jpg";
			_equipMc.source	=	ResConfig.pngPhotoPath + val + ".jpg";
			_equipMc.width	=	_equipMc.height	=	(_nineSize+_nineBlank)*3 - _nineBlank;
		}
		
		/**
		 * 是否可以领取 
		 * @param val
		 * 
		 */
		public function set canGet(val:Boolean):void
		{
			if(_canGet != val)
			{
				if(val)
				{
					//在播放时候不立即显示
					if(!_isPlay)
					{
						//领取特效
						_equipMc.filters	=	[Filter.YELLOW_INNER];
					}
				}
				else
				{
					_equipMc.filters	=	null;
					clearAll();
				}
				_canGet	=	val;
			}
		}
		
		public function get canGet():Boolean
		{
			return _canGet;
		}
		
		/**
		 * 清除状态 (所有为获取，加蒙版)
		 * 
		 */
		public function clearAll():void
		{
//			this.canGet	=	false;
			for(var i:int=0;i<_nineArr.length;i++)
			{
				drawBG(_nineArr[i] as Sprite);
			}
//			_getCount	=	0;
			_getArr		=	[];
		}
	}
}